package com.taoyige.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyige.R;
import com.taoyige.common.BitmapUtil;
import com.taoyige.common.StringUtils;
import com.taoyige.common.UIHelper;

/**
 * 商品详情页详情图片Adapter类
 * 
 * @author peter
 * @version 1.0
 * @created 2014-11-26
 */
public class ListGoodsDetailImagesAdapter extends BaseAdapter {
	private Context context;// 运行上下文
	private List<String> listItems;// 数据集合
	private LayoutInflater listContainer;// 视图容器
	private int itemViewResource;// 自定义项视图源
	private BitmapUtil bmpUtil;

	static class ListItemView { // 自定义控件集合
		public ImageView imageDetail;
	}

	/**
	 * 实例化Adapter
	 * 
	 * @param context
	 * @param data
	 * @param resource
	 */
	public ListGoodsDetailImagesAdapter(Context context, List<String> data, int resource) {
		this.context = context;
		this.listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		//TODO 存在OOM和点击频繁卡顿问题
		this.bmpUtil = new BitmapUtil(BitmapFactory.decodeResource(context.getResources(), 0));
		//this.bmpUtil = new BitmapUtil(BitmapFactory.decodeStream((context.getResources().openRawResource(R.drawable.img_background))));
	}

	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		// 自定义视图
		ListItemView listItemView = null;

		if (convertView == null) {
			// 获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			//convertView.setBackgroundColor(this.context.getResources().getColor(R.color.gray));
			
			
			listItemView = new ListItemView();
			// 获取控件对象
			listItemView.imageDetail = (ImageView) convertView
					.findViewById(R.id.goods_detail_item_image_url_detail);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}

		// 设置文字和图片
		String imageUrl = listItems.get(position);

		if (StringUtils.isEmpty(imageUrl)) {
			listItemView.imageDetail.setImageResource(R.drawable.demo_goods);
		} else {
			bmpUtil.loadBitmap(imageUrl, listItemView.imageDetail);
			//bmpUtil.loadBitmap(imageUrl, listItemView.imageDetail, BitmapFactory.decodeResource(context.getResources(),R.anim.loading));
		}

		return convertView;
	}
}